import 'package:flutter/material.dart';
import 'package:markdown_widget/widget/widget_visitor.dart';

/*class HighlightTextGenerator extends SpanNodeGeneratorWithTag {
  final String searchKeyword;

  HighlightTextGenerator(, {required super.tag, required super.generator}this.searchKeyword);

  @override
  InlineSpan? generate(InlineNode node) {
    final text = node.textContent;
    if (searchKeyword.isEmpty) {
      return TextSpan(text: text, style: node.style);
    }

    // 分割文本并高亮匹配部分
    final spans = <InlineSpan>[];
    int lastIndex = 0;

    // 查找所有匹配的关键词
    final RegExp regex = RegExp(searchKeyword, caseSensitive: false);
    final matches = regex.allMatches(text);

    for (final match in matches) {
      // 添加匹配前的普通文本
      if (match.start > lastIndex) {
        spans.add(TextSpan(
          text: text.substring(lastIndex, match.start),
          style: node.style,
        ));
      }

      // 添加高亮文本
      spans.add(TextSpan(
        text: text.substring(match.start, match.end),
        style: node.style?.copyWith(
          backgroundColor: Colors.yellowAccent, // 高亮背景色
          color: Colors.red, // 高亮文字颜色
        ),
      ));

      lastIndex = match.end;
    }

    // 添加剩余文本
    if (lastIndex < text.length) {
      spans.add(TextSpan(
        text: text.substring(lastIndex),
        style: node.style,
      ));
    }

    return TextSpan(children: spans);
  }
}*/
